package com.infore.dataReceiving.quartz;
import java.util.HashMap;
import java.util.Map;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.infore.dataReceiving.entity.air212.OriginalData;
import com.infore.dataReceiving.netty.client.NettyClient;
import com.infore.dataReceiving.repository.air212.IOrigDataRepository;
import com.infore.dataReceiving.repository.air212.IOriginalDataRepository;
import com.infore.dataReceiving.repository.air212.IRealDataRepository;
import com.infore.dataReceiving.repository.batch.DataBatchImplRepository;
import com.infore.dataReceiving.util.Air212InstructionsHandleUtil;
import com.infore.dataReceiving.util.RedisUtil;

//@Component
public class Air212ScheduledTasks{
	
	private static final Logger logger = LoggerFactory.getLogger(Air212ScheduledTasks.class);
	private static Map<String, String> stationMap = new HashMap<String, String>();
	private static Map<String, String> backstationMap = new HashMap<String, String>();
	
	@Autowired
    private RedisUtil redisUtil;
	
	@Autowired
	private IOriginalDataRepository iOriginalDataRepository;
	
	@Autowired
	private IOrigDataRepository iOrigDataRepository;
	
	
	@Autowired
	private IRealDataRepository iRealTimeDataRepository;
	
	
	@Autowired
	private DataBatchImplRepository dataBatchImplRepository;
	
	
	@Autowired
	@Qualifier("nettyClient")
	private NettyClient nettyClient;
	
	@Autowired
	@Qualifier("scheduler")
    private Scheduler scheduler;
    
    @Autowired
	private QuartzScheduler quartzScheduler;

	
	
	public void GetAirRealTimeData(){

		if(redisUtil==null) {
			System.out.println("redis为空，请联系管理员！");
			logger.error("redis为空，请联系管理员！");
			return;
		}
		while(redisUtil.lGetListSize("originalData")>0) {
			OriginalData data = (OriginalData)redisUtil.leftPop("originalData");
			String msg = "mn="+data.getMn()+"factorCode="+data.getProjectCode()+"dateTime="+data.getReceiveTime();
			if(stationMap.get(msg)==null) {
				stationMap.put(msg, "1");
			}else {
				System.out.println("丢弃重复数据");
				continue;
			}
			System.out.println("获取到实时数据MN："+data.getMn());
			try {
				iOriginalDataRepository.saveAndFlush(data);
			} catch (Exception e) {
				System.out.println("重复实时数据："+msg);
				logger.error("重复实时数据："+msg);
			}
		}
		stationMap = new HashMap<String, String>();
	}
	
	
//	public void GetAirRealTimeData(){
//
//		if(redisUtil==null) {
//			System.out.println("redis为空，请联系管理员！");
//			logger.error("redis为空，请联系管理员！");
//			return;
//		}
//		while(redisUtil.lGetListSize("originalData")>0) {
//			OrigData data = (OrigData)redisUtil.leftPop("originalData");
//			String msg = "mn="+data.getMoniEquiId()+"factorCode="+data.getMoniProjName()+"dateTime="+data.getTime();
//			if(stationMap.get(msg)==null) {
//				stationMap.put(msg, "1");
//			}else {
//				System.out.println("丢弃重复数据");
//				continue;
//			}
//			System.out.println("获取到实时数据MN："+data.getMoniEquiId());
//			try {
//				iOrigDataRepository.saveAndFlush(data);
//			} catch (Exception e) {
//				System.out.println("重复实时数据："+msg);
//				logger.error("重复实时数据："+msg);
//			}
//		}
//		stationMap = new HashMap<String, String>();
//	}
	
	public void GetAirBackData(){

		if(redisUtil==null) {
			System.out.println("redis为空，请联系管理员！");
			logger.error("redis为空，请联系管理员！");
			return;
		}
		int count = 0;
		while(redisUtil.lGetListSize("backData")>0) {
			OriginalData data = (OriginalData)redisUtil.leftPop("backData");
			String msg = "mn="+data.getMn()+"factorCode="+data.getProjectCode()+"dateTime="+data.getReceiveTime();
			if(count>500) {
				try {
					count = 0;
					Thread.sleep(5*1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			if(backstationMap.get(msg)==null) {
				backstationMap.put(msg, "1");
			}else {
				System.out.println("丢弃重复补遗数据");
				continue;
			}
			System.out.println("获取到补遗数据MN："+data.getMn());
			try {
				iOriginalDataRepository.saveAndFlush(data);
			} catch (Exception e) {
				System.out.println("重复补遗数据："+msg);
				logger.error("重复补遗数据："+msg);
			}
			count++;
		}
		backstationMap = new HashMap<String, String>();
//		Air212InstructionsHandleUtil.dateMap.clear();
	}
	
	
//	public void GetAirBackData(){
//
//		if(redisUtil==null) {
//			System.out.println("redis为空，请联系管理员！");
//			logger.error("redis为空，请联系管理员！");
//			return;
//		}
//		while(redisUtil.lGet("realDatas",0,-1).size()>0) {
//			RTEquipment equipment = (RTEquipment)redisUtil.leftPop("realDatas");
//
//			Map<Long, Long> dataMap = (Map<Long, Long>)redisUtil.get("dataMap");
//			if(dataMap==null) {
//				dataMap = new HashMap<Long, Long>();
//				List<RealData> dataList = iRealTimeDataRepository.findAll();
//				for(RealData realData : dataList) {
//					JSONObject jsonObject = new JSONObject();
//					String bag = realData.getBag();
//					RTEquipment equipment1 = jsonObject.parseObject(bag,RTEquipment.class);
//					dataMap.put(equipment1.getId(), realData.getId());
//				}
//			}
//			
//			try {
//				Long ids = dataMap.get(equipment.getId());
//				JSONObject jsonObjects = new JSONObject();
//				String msg = jsonObjects.toJSONString(equipment);
//				RealData data1 = new RealData();
//				if(ids!=null) {
//					data1.setId(ids);
//				}
//				data1.setBag(msg);
//				iRealTimeDataRepository.saveAndFlush(data1);
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
//		stationMap = new HashMap<String, String>();
//	}
	
	 /**
     * 开始执行所有任务
     * 
     * @throws SchedulerException
     */
    public void AddJob() throws SchedulerException {
    	Map<String, String> czMap = Air212InstructionsHandleUtil.czMap;
    	quartzScheduler.addJob(scheduler,czMap);
    }
    
    
    
	 /**
     * 向市平台传输数据
     * 
     * @throws SchedulerException
     */
    public void RomoteData() throws SchedulerException {

		if(redisUtil==null) {
			System.out.println("redis为空，请联系管理员！");
			logger.error("redis为空，请联系管理员！");
			return;
		}
		while(redisUtil.lGetListSize("romoteData")>0) {
			String instruction = (String)redisUtil.leftPop("romoteData");
			try {
				System.out.println("向市平台发送数据"+instruction);
				nettyClient.sendMessage("10.33.50.69", 51000, instruction);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
}
